// Copyright (c) 2021 Presto Labs Pte. Ltd.
// Author: daniel

#pragma once

#include <memory>
#include <string>

#include "coin2/exchange/base/api_base/asio_http_context.h"
#include "coin2/exchange/base/order/order.h"
#include "coin2/exchange/bitbank/api_util/auth_key.h"

namespace coin2::exchange::order {
class BitbankRestParams;
class OrderConnectionConfig;
}  // namespace coin2::exchange::order

namespace coin2::exchange::base::order {
struct OrderExecution;
struct OrderSpec;
}  // namespace coin2::exchange::base::order

class BitbankNativePrivateClient : public coin2::exchange::base::order::NativePrivateClient {
 public:
  using Base = coin2::exchange::base::order::NativePrivateClient;
  using RestParams = coin2::exchange::order::BitbankRestParams;
  using NativeClientConfig = coin2::exchange::order::OrderConnectionConfig;
  using OrderExecution = coin2::exchange::base::order::OrderExecution;
  using OrderSpec = coin2::exchange::base::order::OrderSpec;
  using IOrderContext = coin2::exchange::base::order::IOrderContext;
  using HttpContextPtr = std::shared_ptr<AsioHttpContext>;
  using ActionType = coin2::exchange::base::order::NativePrivateClient::ActionType;

 public:
  std::shared_ptr<AsioHttpContext> SubmitOrderExecution(const OrderExecutionProto& order);
  std::shared_ptr<AsioHttpContext> SubmitRestQuery(const RestQueryProto& query);

 public:
  explicit BitbankNativePrivateClient(
      const MarketExchangeApi& mea,
      const NativeClientConfig& config);

  HttpContextPtr QueryAccountBalance();
  HttpContextPtr QueryOrders(const RestParams& params);
  HttpContextPtr QueryTrades(const RestParams& params);
  HttpContextPtr QueryOrderInfo(const RestParams& params);
  HttpContextPtr QueryOrdersInfo(const RestParams& params);
  HttpContextPtr PlaceOrder(const RestParams& params);
  HttpContextPtr CancelSingleOrder(const RestParams& params);
  HttpContextPtr CancelBatchOrders(const RestParams& params);

  HttpContextPtr PlaceOrderNorm(const OrderExecution&) override;
  HttpContextPtr CancelOrderNorm(const OrderExecution&) override;

  RestParams GetRestOrderParams(const OrderSpec& order_spec, int64_t proc_order_id);
  RestParams GetRestCancelParams(const IOrderContext& order_context);

  BaseRestAuth* GetAuth() const override {
    auth_index_++;
    if (auth_index_ >= auths_.GetSize()) {
      auth_index_ = 0;
    }
    return auths_.GetNth(auth_index_);
  }

 private:
  mutable int auth_index_{0};
  const std::string path_query_account_balance_ = "/v1/user/assets";
  const std::string path_query_orders_ = "/v1/user/spot/active_orders";
  const std::string path_query_trades_ = "/v1/user/spot/trade_history";
  const std::string path_query_order_info_ = "/v1/user/spot/order";
  const std::string path_query_orders_info_ = "/v1/user/spot/orders_info";
  const std::string path_place_order_ = "/v1/user/spot/order";
  const std::string path_cancel_single_order_ = "/v1/user/spot/cancel_order";
  const std::string path_cancel_batch_orders_ = "/v1/user/spot/cancel_orders";
};
